package gui.dialogs;

import game.GameField;
import gui.components.MImage;
import gui.game.GUI;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

import utility.WindowManipulator;
import core.Configuration;

public class BuyDialog extends GameDialog implements ActionListener{
	
	private static int DIALOG_WIDTH = 600;
	private static int DIALOG_HEIGHT = 300;
	JButton buyBtn;
	JButton cancelBtn;
	Window parent;
	
	static{
		DIALOG_WIDTH = (int)(DIALOG_WIDTH*Configuration.zoomConst);
		DIALOG_HEIGHT = (int)(DIALOG_HEIGHT*Configuration.zoomConst);
	}
		
	public BuyDialog(Window parent, GameField field){
		super(parent);
		this.setSize(DIALOG_WIDTH,DIALOG_HEIGHT);
		
		this.parent = parent;
		Container c = this.getContentPane();	
		
		
		MImage bkg = null;
		try {
			bkg = new MImage(Configuration.IMAGE_FOLDER_ILLUSTRATION+"dialog_bkg.png",new Dimension(DIALOG_WIDTH,DIALOG_HEIGHT),true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		bkg.setBounds(0, 0, DIALOG_WIDTH, DIALOG_HEIGHT);
		
		
		JLabel title1 = new JLabel("DO YOU WANT TO BUY...?");
		title1.setForeground(new Color(228,42,42));
		title1.setFont(new Font("Sans Serif",Font.PLAIN,26));
		title1.setBounds((int)(DIALOG_WIDTH*0.35),(int)(DIALOG_HEIGHT*0.25), 340, 30);
		
		JLabel title2 = new JLabel(field.getName());
		title2.setForeground(new Color(128,128,128));
		title2.setFont(new Font("Arial",Font.BOLD,32));
		title2.setBounds((int)(DIALOG_WIDTH*0.33),(int)(DIALOG_HEIGHT*0.4), 360, 30);
		title2.setHorizontalAlignment(JLabel.CENTER);
		
		String cost = String.valueOf((double)field.getCost()/1000000);
		
		JLabel title3 = new JLabel("$ "+cost+"M");
		title3.setForeground(Color.BLACK);
		title3.setFont(new Font("Sans Serif",Font.BOLD,30));
		title3.setBounds((int)(DIALOG_WIDTH*0.03),(int)(DIALOG_HEIGHT*0.74), 125, 50);
		title3.setHorizontalAlignment(JLabel.CENTER);
		
		Dimension iconSize = new Dimension(110,160);
		Image im = field.getIcon().getImage();
		
		Image rotatedImage = im;
		if(field.getOrientation()==field.DOWN){
			rotatedImage = MImage.rotateImage(im, 180);
		}else if(field.getOrientation()==field.RIGHT){
			rotatedImage = MImage.rotateImage(im, 270);
		}else if(field.getOrientation()==field.LEFT){
			rotatedImage = MImage.rotateImage(im, 90);
		}
		
		MImage preview = new MImage(rotatedImage,iconSize,true);
		preview.setBounds((int)(DIALOG_WIDTH*0.04),(int)(DIALOG_HEIGHT*0.12), 110, 160);

		buyBtn = new JButton("Buy");
		buyBtn.setBounds((int)(DIALOG_WIDTH*0.4),(int)(DIALOG_HEIGHT*0.74), 160, 50);
		buyBtn.setFont(new Font("Sans Serif",Font.PLAIN,25));
		buyBtn.setVisible(true);
		buyBtn.addActionListener(this);
		
		cancelBtn = new JButton("Cancel");
		cancelBtn.setBounds((int)(DIALOG_WIDTH*0.7),(int)(DIALOG_HEIGHT*0.74), 160, 50);
		cancelBtn.setFont(new Font("Sans Serif",Font.PLAIN,25));
		cancelBtn.setVisible(true);
		cancelBtn.addActionListener(this);

		c.add(cancelBtn);
		c.add(buyBtn);
		c.add(title3);
		c.add(title2);
		c.add(title1);
		c.add(preview);
		c.add(bkg);
		WindowManipulator.movePositionToCenter(parent,this);
		
		this.setVisible(true);			
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buyBtn){
			GameDialog.setLastBoolOption(true);
		} else if(e.getSource()==cancelBtn){
			GameDialog.setLastBoolOption(false);
		}
		parent.setEnabled(true);
		this.dispose();
	}
}
